home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / foo / gld-pinsn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-05  |  6.5 KB  |  291 lines

  1. /* Print GOULD RISC instructions for GDB, the GNU debugger.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. GDB is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GDB is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GDB; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <a.out.h>
  22.  
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "symtab.h"
  26. #include "frame.h"
  27. #include "opcode.h"
  28.  
  29. /* GOULD RISC instructions are never longer than this many bytes.  */
  30. #define MAXLEN 4
  31.  
  32. /* Number of elements in the opcode table.  */
  33. #define NOPCODES (sizeof gld_opcodes / sizeof gld_opcodes[0])
  34.  
  35.  
  36. /* Print the GOULD instruction at address MEMADDR in debugged memory,
  37.    on STREAM.  Returns length of the instruction, in bytes.  */
  38.  
  39. int
  40. print_insn (memaddr, stream)
  41.     CORE_ADDR memaddr;
  42.     FILE *stream;
  43. {
  44.     unsigned char buffer[MAXLEN];
  45.     register int i;
  46.     register char *d;
  47.     register int bestmask;
  48.     unsigned best;
  49.     int temp, index, bestlen;
  50.  
  51.     read_memory (memaddr, buffer, MAXLEN);
  52.  
  53.     bestmask = 0;
  54.     index = -1;
  55.     best = 0xffffffff;
  56.     for (i = 0; i < NOPCODES; i++)
  57.     {
  58.         register unsigned int opcode = gld_opcodes[i].opcode;
  59.         register unsigned int mask = gld_opcodes[i].mask;
  60.         register unsigned int len = gld_opcodes[i].length;
  61.         register unsigned int test;
  62.  
  63.         /* Get possible opcode bytes into integer */
  64.         test = buffer[0] << 24;
  65.         test |= buffer[1] << 16;
  66.         test |= buffer[2] << 8;
  67.         test |= buffer[3];
  68.  
  69.         /* Mask with opcode and see if match */
  70.         if ((opcode & mask) == (test & mask))
  71.         {
  72.             /* See if second or third match */
  73.             if (index >= 0)
  74.             {
  75.                 /* Take new one if it looks good */
  76.                 if (bestlen == MAXLEN && len == MAXLEN)
  77.                 {
  78.                     /* See if lower bits matched */
  79.                     if (((bestmask & 3) == 0) &&
  80.                         ((mask & 3) != 0))
  81.                     {
  82.                         bestmask = mask;
  83.                         bestlen = len;
  84.                         best = test;
  85.                         index = i;
  86.                     }
  87.                 }
  88.             }
  89.             else
  90.             {
  91.                 /* First match, save it */
  92.                 bestmask = mask;
  93.                 bestlen = len;
  94.                 best = test;
  95.                 index = i;
  96.             }
  97.         }
  98.     }
  99.  
  100.     /* Handle undefined instructions.  */
  101.     if (index < 0)
  102.     {
  103.         fprintf (stream, "undefined   0%o",(buffer[0]<<8)+buffer[1]);
  104.         return 2;
  105.     }
  106.  
  107.     /* Print instruction name */
  108.     fprintf (stream, "%-12s", gld_opcodes[index].name);
  109.  
  110.     /* Adjust if short instruction */
  111.     if (gld_opcodes[index].length < 4)
  112.     {
  113.         best >>= 16;
  114.         i = 0;
  115.     }
  116.     else
  117.     {
  118.         i = 16;
  119.     }
  120.  
  121.     /* Dump out instruction arguments */
  122.       for (d = gld_opcodes[index].args; *d; ++d)
  123.     {
  124.         switch (*d)
  125.         {
  126.         case 'f':
  127.             fprintf (stream, "%d",  (best >> (7 + i)) & 7);
  128.             break;
  129.         case 'r':
  130.             fprintf (stream, "r%d", (best >> (7 + i)) & 7);
  131.             break;
  132.         case 'R':
  133.             fprintf (stream, "r%d", (best >> (4 + i)) & 7);
  134.             break;
  135.         case 'b':
  136.             fprintf (stream, "b%d", (best >> (7 + i)) & 7);
  137.             break;
  138.         case 'B':
  139.             fprintf (stream, "b%d", (best >> (4 + i)) & 7);
  140.             break;
  141.         case 'v':
  142.             fprintf (stream, "b%d", (best >> (7 + i)) & 7);
  143.             break;
  144.         case 'V':
  145.             fprintf (stream, "b%d", (best >> (4 + i)) & 7);
  146.             break;
  147.         case 'X':
  148.             temp = (best >> 20) & 7;
  149.             if (temp)
  150.             fprintf (stream, "r%d", temp);
  151.             else
  152.             putc ('0', stream);
  153.             break;
  154.         case 'A':
  155.             temp = (best >> 16) & 7;
  156.             if (temp)
  157.             fprintf (stream, "(b%d)", temp);
  158.             break;
  159.         case 'S':
  160.             fprintf (stream, "#%d", best & 0x1f);
  161.             break;
  162.         case 'I':
  163.             fprintf (stream, "#%x", best & 0xffff);
  164.             break;
  165.         case 'O':
  166.             fprintf (stream, "%x", best & 0xffff);
  167.             break;
  168.         case 'h':
  169.             fprintf (stream, "%d", best & 0xfffe);
  170.             break;
  171.         case 'd':
  172.             fprintf (stream, "%d", best & 0xfffc);
  173.             break;
  174.         case 'T':
  175.             fprintf (stream, "%d", (best >> 8) & 0xff);
  176.             break;
  177.         case 'N':
  178.             fprintf (stream, "%d", best & 0xff);
  179.             break;
  180.         default:
  181.             putc (*d, stream);
  182.             break;
  183.         }
  184.     }
  185.  
  186.     /* Return length of instruction */
  187.       return (gld_opcodes[index].length);
  188. }
  189.  
  190. /*
  191.  * Find the number of arguments to a function.
  192.  */
  193. findarg(frame)
  194.     struct frame_info *frame;
  195. {
  196.     register struct symbol *func;
  197.     register unsigned pc;
  198.  
  199. #ifdef notdef
  200.     /* find starting address of frame function */
  201.     pc = get_pc_function_start (frame->pc);
  202.  
  203.     /* find function symbol info */
  204.     func = find_pc_function (pc);
  205.  
  206.     /* call blockframe code to look for match */
  207.     if (func != NULL)
  208.                 return (func->value.block->nsyms / sizeof(int));
  209. #endif
  210.  
  211.         return (-1);
  212.  
  213. /*
  214.  * In the case of the NPL, the frame's norminal address is Br2 and the 
  215.  * previous routines frame is up the stack X bytes.  Finding out what
  216.  * 'X' is can be tricky.
  217.  *
  218.  *    1.) stored in the code function header xA(Br1).
  219.  *    2.) must be careful of recurssion.
  220.  */
  221. FRAME_ADDR
  222. findframe(thisframe)
  223.     FRAME thisframe;
  224. {
  225.     register FRAME_ADDR pointer;
  226. #if 0    
  227.     struct frame_info *frame;
  228.     FRAME_ADDR framechain();
  229.  
  230.     /* Setup toplevel frame structure */
  231.     frame->pc = read_pc();
  232.     frame->next_frame = 0;
  233.     frame->frame = read_register (SP_REGNUM);    /* Br2 */
  234.  
  235.     /* Search for this frame (start at current Br2) */
  236.     do
  237.     {
  238.     pointer = framechain(frame);
  239.     frame->next_frame = frame->frame;
  240.     frame->frame = pointer;
  241.     frame->pc = FRAME_SAVED_PC(frame);
  242.     }
  243.     while (frame->next_frame != thisframe);
  244. #endif
  245.  
  246.     pointer = framechain (thisframe);
  247.  
  248.     /* stop gap for now, end at __base3 */
  249.     if (thisframe->pc == 0)
  250.     return 0;
  251.  
  252.     return pointer;
  253. }
  254.  
  255. /*
  256.  * Gdb front-end and internal framechain routine.
  257.  * Go back up stack one level.  Tricky...
  258.  */
  259. FRAME_ADDR
  260. framechain(frame)
  261.     register struct frame_info *frame;
  262. {
  263.     register CORE_ADDR func, prevsp;
  264.     register unsigned value;
  265.  
  266.     /* Get real function start address from internal frame address */
  267.     func = get_pc_function_start(frame->pc);
  268.  
  269.     /* If no stack given, read register Br1 "(sp)" */
  270.     if (!frame->frame)
  271.     prevsp = read_register (SP_REGNUM);
  272.     else
  273.     prevsp = frame->frame;
  274.  
  275.     /* Check function header, case #2 */
  276.     value = read_memory_integer (func, 4);
  277.     if (value)
  278.     {
  279.     /* 32bit call push value stored in function header */
  280.     prevsp += value;
  281.     }
  282.     else
  283.     {
  284.     /* read half-word from suabr at start of function */
  285.     prevsp += read_memory_integer (func + 10, 2);
  286.     }
  287.  
  288.     return (prevsp);
  289. }
  290.